<?php

namespace Admin\Controller;



class ManagerController extends CommonController
{
    //公司级
    public function company_list()
    {
        //首先根据登陆的id查询其等级，和负责的风险点
        $user_id = $_SESSION['userid'];
        $admin_id = $_SESSION['admin_id'];
        $level_id = $_SESSION['level_id'];
        $object_ids = $_SESSION['object_ids'];


        //没有危险源的进行提示
        if (empty($object_ids)) {
            $this->error('当前登录角色没有分配危险源！', U('Test/test_list'));
        }

        //首先判断该用户的等级是否与当前菜单等级是否匹配，如公司级则要求用户等级为公司级

        if ($level_id == 1 || $admin_id == 1) {
            //查询当前用户负责的风险点，
            //  $sql = " select id,  name from   xc_danger where id=(select danger_id from xc_object where  object_level_id=1 and status=1)";
            $sql = " select id,  name from   xc_danger where id in (  select  DISTINCT(danger_id)   from xc_object where  status=1 and object_id in ( " . $object_ids . "))";
            $Data = M('danger'); //实例化Data数据模型
            $list = $Data->query($sql);
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    //根据风险点ID查询，风险等级为1的
                    //  $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                    $sql1 = "select *  from xc_object where  status=1 and object_id in ( " . $object_ids . ") and danger_id =" . $v['id'];
                    $Data1 = M('object'); //实例化Data数据模型
                    $res2 = $Data1->query($sql1);
                    $list[$k]['voo'] = $res2;
                }
            }
            $this->assign('list', $list);
            $this->display('company_list');

        } else {
            //   echo  $level_id;
            $this->error('当前登录角色不属于公司级！', U('Test/test_list'));

        }
    }

    public function getParentId($user_id)
    {
        //  $sql="select  parent_id  from xc_user where   status=1  and  user_id = ".$user_id;
        $User = M("User"); // 实例化User对象
        $parentId = $User->where((['user_id' => $user_id]))->getField('parent_id');
        return $parentId;
    }


    public function company_do()
    {
        //打开详细页面根据危险源ID获取该危险源详细信息
        if (IS_GET) {

            $object_id = $_GET['object_id'];
            //写入session 备用
            session('object_id',  $object_id);
            //1、查询风险点名称
            $where['object_id']=$object_id;
            $where['status']=1;
            $danger = M('danger')->where($where)->find();
//2.责任人
            $sql='SELECT level_id,username,object_ids  FROM xc_user  where object_ids like  "%'. $object_id.'%" or object_ids like  "%'.$object_id.',%"  ORDER BY level_id desc';
            $zerenrens= M('user')->query($sql);
            //echo  $zerenrens[1]['username'];

            $info = M('Object')->where(['object_id' => $object_id])->find();
            $this->assign('zerenrens',$zerenrens);

         //   echo $info['object_level_id'];
            $this->assign('danger',$danger);
            $this->assign('info', $info);
            $this->display('company_do');
        }

        if (IS_POST) {

            //如果是上传照片
          /*  if (isset($_POST['uploadimage'])) {
               // echo '上传照片';
          //   upload();
               $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = '/pc/';// 设置附件上传目录.date("Y-m-d").\'/
                $upload->saveRule = 'uniqid';
                $upload->thumbMaxWidth = '200,200';

                $img_url=   $upload->rootPath.$upload->savePath;
             //   echo $img_url;
               $info = $upload->upload();

                if (!$info) {// 上传错误提示错误信息
                    $this->error($upload->getErrorMsg());
                } else {// 上传成功
                   // echo $info['savename'];
                    //取得成功上传的文件信息
                   // $uploadList = $upload->getUploadFileInfo();
                  //  $_POST['image'] = $uploadList[0]['savename'];
                 // var_dump($info);
                    //array(1) { ["photo"]=> array(9) { ["name"]=> string(22) "静态文字修改.jpg" ["type"]=> string(10) "image/jpeg" ["size"]=> int(167821) ["key"]=> string(5) "photo" ["ext"]=> string(3) "jpg" ["md5"]=> string(32) "b8d2a5ffa1b77ead1c1ea7dffe1e5d66" ["sha1"]=> string(40) "9cf326a1237efc431e16a14186a1d66ef0b43976" ["savename"]=> string(17) "5a1ac7f665268.jpg" ["savepath"]=> string(16) "./pc/2017-11-26/" } }
              //    echo $info["photo"]["savename"]."<br>";
                 //   echo $info["photo"]["savepath"]  ;
                    $img_url='Uploads'.$info["photo"]["savepath"].$info["photo"]["savename"];
           // echo        $img_url;
         //     $this->success($info[0]['savename']);
                }
            }*/
            if (isset($_POST['guidang']))      {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = '/pc/';// 设置附件上传目录.date("Y-m-d").\'/
                $upload->saveRule = 'uniqid';
                $upload->thumbMaxWidth = '200,200';

                $info = $upload->upload();
                //'Uploads'.$info["photo"]["savepath"].
                $img_url=$info["photo"]["savename"];
                //登陆用户ID
            $user_id = $_SESSION['user_id'];
            //当前处理人ID
            $deal_user_id = $user_id;
            //获取当前客户的上一级用户
            $User = M("User"); // 实例化User对象
            $pending_user_id = $User->where((['user_id' => $user_id]))->getField('parent_id');
            $status = 1;
            $img_url = 'Uploads'.$info["photo"]["savepath"].$info["photo"]["savename"];
            $img_url1 = '';
            $img_url2 = '';
            $img_url3 = '';
            $video = '';
            $deal =I('post.deal');
            $desc = I('post.desc');
            //根据选择判断是否选择了有风险  $is_danger = I('is_danger');
            $is_danger = $_POST['is_danger'];
            $object_id = $_SESSION['object_id'];
            $creat_time = time();
            $zhongduan = '2';

            $data = [
                'push_user_id' => $user_id,
                'deal_user_id' => $user_id,
                'pending_user_id' => 0, //
                'img_url' =>  $img_url,
                'video' =>  $video,
                'deal' =>  $deal,
                'desc' =>  $desc,
                'is_danger' =>  $is_danger,
                'object_id' =>  $object_id,
                'zhongduan' =>  $zhongduan,
                'create_time' => time(),
                'status' => 0,  //归档为0，上级处理人为0
            ];
            $res = M('Push_record')->add($data);
            if (empty($res)) {
                $this->error('添加失败！','' );//U('Maneger/add_msg')
            }
            else
            {
                $this->success('操作成功！');

            }

        }

            if (isset($_POST['piban']))      {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = '/pc/';// 设置附件上传目录.date("Y-m-d").\'/
                $upload->saveRule = 'uniqid';
                $upload->thumbMaxWidth = '200,200';

                $info = $upload->upload();
                //'Uploads'.$info["photo"]["savepath"].
                $img_url=$info["photo"]["savename"];
                //登陆用户ID
                $user_id = $_SESSION['user_id'];
                //当前处理人ID
                $deal_user_id = $user_id;
                //获取当前客户的上一级用户
                $User = M("User"); // 实例化User对象
                $pending_user_id = $User->where((['user_id' => $user_id]))->getField('parent_id');
                $status = 1;
                $img_url = 'Uploads'.$info["photo"]["savepath"].$info["photo"]["savename"];
                $img_url1 = '';
                $img_url2 = '';
                $img_url3 = '';
                $video = '';
                $deal =I('post.deal');
                $desc = I('post.desc');
                //根据选择判断是否选择了有风险  $is_danger = I('is_danger');
                $is_danger = $_POST['is_danger'];
                $object_id = $_SESSION['object_id'];
                $creat_time = time();
                $zhongduan = '2';

                $data = [
                    'push_user_id' => $user_id,
                    'deal_user_id' => $user_id,
                    'pending_user_id' => $pending_user_id, //
                    'img_url' =>  $img_url,
                    'video' =>  $video,
                    'deal' =>  $deal,
                    'desc' =>  $desc,
                    'is_danger' =>  $is_danger,
                    'object_id' =>  $object_id,
                    'zhongduan' =>  $zhongduan,
                    'create_time' => time(),
                    'status' => 1,  //归档为0，上级处理人为0
                ];
                $res = M('Push_record')->add($data);
                if (empty($res)) {
                    $this->error('添加失败！','' );//U('Maneger/add_msg')
                }
                else
                {
                    $this->success('操作成功！');

                }

            }

     }
    }
// 文件上传
    public function upload() {
        if(I('post.ImgURL','','htmlspecialchars')!=""){
          $uploads="Uploads";//保存路径
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Uploads/'; // 设置附件上传根目录
            $upload->savePath = './pc/';// 设置附件上传目录.date("Y-m-d").\'/
            // 上传单个文件
            $info   =   $upload->uploadOne($_FILES['photo']);

            if(!$info) {
                // 上传错误提示错误信息
                $this->error($upload->getError());
            }
            else
            {
                //上传成功提示成功信息
                $this->success('上传成功');
            }
        }
        else
        {
            $this->error('请选择上传文件');
        }

    }

//专业级
    public function part_list()
    {
        //首先根据登陆的id查询其等级，和负责的风险点
        $user_id=$_SESSION['userid'];
        $admin_id =$_SESSION['admin_id'];
        $level_id=$_SESSION['level_id'];
        $object_ids=$_SESSION['object_ids'];
        //没有危险源的进行提示
        if(  empty($object_ids))
        {
            $this->error('当前登录角色没有分配危险源！',U('Test/test_list'));
        }
//首先判断该用户的等级是否与当前菜单等级是否匹配，如公司级则要求用户等级为公司级
        if($level_id==2 || $admin_id==1 )
        {
            //查询当前用户负责的风险点，
            //  $sql = " select id,  name from   xc_danger where id=(select danger_id from xc_object where  object_level_id=1 and status=1)";
            $sql = " select id,  name from   xc_danger where id in(  select  DISTINCT(danger_id)   from xc_object where  status=1 and object_id in ( ".$object_ids."))";
            $Data = M('danger'); //实例化Data数据模型
            $list = $Data->query($sql);
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    //根据风险点ID查询，风险等级为1的
                    //  $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                    $sql1="select *  from xc_object where  status=1 and object_id in ( ".$object_ids.") and danger_id =".$v['id'];
                    $Data1 = M('object'); //实例化Data数据模型
                    $res2 =  $Data1->query($sql1);
                    $list[$k]['voo'] = $res2;
                }
            }
            $this->assign('list', $list);
            $this->display('company_list');

        }
        else
        {
            //   echo  $level_id;
            $this->error('当前登录角色不属于专业级！',U('Test/test_list'));

        }


        /*

        //部门表
        $list = $this->lists('part', 'status = 1', ' id asc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                //查询部门级风险点，等级为2的危险源,并且责任单位(对应车间，这里为专业级)属于对应专业级的ID
                $sql = " select id,  name from   xc_danger where id in (select danger_id from xc_object where  object_level_id=2 and status=1)";
                $Data = M('danger'); //实例化Data数据模型
                $res2 = $Data->query($sql);
                $list[$k]['voo'] = $res2;
                /*  if(  $list[$k]['voo'])
                   {
                       foreach (  $list[$k]['voo'] as $k => $v)
                       {
                           //查询部门级风险点下风险点下对应的危险源为2级的 object_level_id为2的危险源
                           //根据风险点ID查询，风险等级为2的无数据2的
                       }
                   }


            }

            /*
            //车间表
            $res2 = M('workshop')->where(['part_id' => $v['id']])->select();
            $list[$k]['voo'] = $res2;

        }

        // $sql= "  select danger_id from xc_object where  object_level_id=2 and status=1 and danger_id=".$v['id'];
        $sql = "  select danger_id ,object_id,name from xc_object where  object_level_id=2 and status=1 and danger_level_id=2  ";
        $Data = M('danger'); //实例化Data数据模型
        $res3 = $Data->query($sql);
        $this->assign('test', $res3);// 赋值数据集
        $this->assign('list', $list);// 赋值数据集


        $this->display('part_list');

*/

        /*
    $sql='select  *  from xc_workshop where status = 1';
   // and part_id =';
    //$sql= $sql.$vo['id'];
    $Data = M('workshop'); //实例化Data数据模型
    $res2= $Data->query($sql);
    // $res2 = $this->lists('workshop', $where, ' id asc');
    if ($res2) {
        foreach ($res2 as $k1 => $sub) {
            // $res2[$k1]['name'] = M('workshop')->where(['part_id' => $v['id']])->getField('name');
            echo  count($res2);
        }
        */
        //  var_dump($res2);

        //    $this->assign('res2', $res2);

        /*
                $list = $this->lists('part','status = 1', ' id asc');
                if (!empty($list)) {
                    foreach ($list as $k=>$v) {
                        //  $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                        //$list[$k]['danger_name'] =  M('danger')->where(['id' => $v['danger_id']])->getField('name');
                    }
                }
               $this->assign('list', $list);
        */
    }


    //车间级
    public function workshop_list()
    {
        //首先根据登陆的id查询其等级，和负责的风险点
        $user_id=$_SESSION['userid'];
        $admin_id =$_SESSION['admin_id'];
        $level_id=$_SESSION['level_id'];
        $object_ids=$_SESSION['object_ids'];
        //没有危险源的进行提示
        if(  empty($object_ids))
        {
            $this->error('当前登录角色没有分配危险源！',U('Test/test_list'));
        }
        //首先判断该用户的等级是否与当前菜单等级是否匹配，如公司级则要求用户等级为公司级
        if($level_id==3 || $admin_id==1 )
        {
            //查询当前用户负责的风险点，
            //  $sql = " select id,  name from   xc_danger where id=(select danger_id from xc_object where  object_level_id=1 and status=1)";
            $sql = " select id,  name from   xc_danger where id in (  select  DISTINCT(danger_id)   from xc_object where  status=1 and object_id in ( ".$object_ids."))";
            $Data = M('danger'); //实例化Data数据模型
            $list = $Data->query($sql);
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    //根据风险点ID查询，风险等级为1的
                    //  $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                    $sql1="select *  from xc_object where  status=1 and object_id in ( ".$object_ids.") and danger_id =".$v['id'];
                    $Data1 = M('object'); //实例化Data数据模型
                    $res2 =  $Data1->query($sql1);
                    $list[$k]['voo'] = $res2;
                }
            }
            $this->assign('list', $list);
            $this->display('company_list');

        }
        else
        {
            //   echo  $level_id;
            $this->error('当前登录角色不属于车间级！',U('Test/test_list'));

        }
        /*
        //车间表
        $list = $this->lists('workshop', 'status = 1', ' id asc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                //查询部门级风险点，等级为2的危险源,并且责任单位(对应车间，这里为专业级)属于对应专业级的ID
                $sql = " select id,  name from   xc_danger where id in (select danger_id from xc_object where  object_level_id=3 and status=1)";
                $Data = M('danger'); //实例化Data数据模型
                $res2 = $Data->query($sql);
                $list[$k]['voo'] = $res2;
            }
            $sql = "  select danger_id ,object_id,name from xc_object where  object_level_id=3 and status=1  ";
            $Data = M('danger'); //实例化Data数据模型
            $res3 = $Data->query($sql);
            $this->assign('test', $res3);// 赋值数据集
            $this->assign('list', $list);// 赋值数据集
                $this->display('workshop_list');

            }
        */
        }

}

